package cn.wolfcode.shop.redis;

import jdk.nashorn.internal.ir.ReturnNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Configuration
@ConditionalOnClass({JedisPool.class, Jedis.class})
@EnableConfigurationProperties(RedisProperties.class)
public class RedisAutoConfiguration {


    @Bean
    @ConditionalOnMissingBean
    public JedisPool jedisPool(RedisProperties redisProperties){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(redisProperties.getPoolMaxTotal());
        config.setMaxIdle(redisProperties.getPoolMaxIdle());
        config.setMaxWaitMillis(redisProperties.getPoolMaxWait()*1000);
        JedisPool jedisPool = new JedisPool(config,redisProperties.getHost(),redisProperties.getPort(),redisProperties.getTimeout(),redisProperties.getPassword());
        return jedisPool;
    }

    @Bean
    public Jedis jedis(){
        return new Jedis();
    }

    @Bean
    public RedisService redisService(){
        return new RedisService();
    }
}
